<template>
	<view class="team-page">
		<view class="top-box">
			<view class="box">
				<image v-if="userInfo.level == 0" class="bg" src="" mode="widthFix"></image>
				<image v-else-if="userInfo.level == 1" class="bg" src="@/static/VIP-bg.png" mode="widthFix"></image>
				<image v-else-if="userInfo.level == 2" class="bg" src="@/static/SVIP-bg.png" mode="widthFix"></image>
				<image v-else-if="userInfo.level == 3" class="bg" src="@/static/PLUS-bg.jpg" mode="widthFix"></image>
				<view class="content">
					<view class="user-info">
						<image class="avatar" :src="userInfo.avatar" mode="aspectFill"></image>
						<view class="info">
							<view class="name">{{ userInfo.name }}</view>
							<view class="grade">
								<text v-if="userInfo.level == 0">普通会员</text>
								<text v-else-if="userInfo.level == 1">VIP</text>
								<text v-else-if="userInfo.level == 2">SVIP</text>
								<text v-else-if="userInfo.level == 3">PLUS</text>
							</view>
						</view>
					</view>
					<view class="id">ID：{{ userInfo.id }}</view>
				</view>
			</view>
		</view>
		<view class="tabs-box">
			<view class="item" :class="{ 'active': tabCurrent == 'zhi' }" @click="tabChange('zhi')">直推</view>
			<view class="item" :class="{ 'active': tabCurrent == 't_zhi' }" @click="tabChange('t_zhi')">贡献</view>
			<view class="item" :class="{ 'active': tabCurrent == 'sanxia' }" @click="tabChange('sanxia')">团队</view>
		</view>
		<view class="auxiliary-box" v-if="tabCurrent == 'zhi' || tabCurrent == 't_zhi'">
			<view class="item" :class="{ 'active': auxiliaryCurrent == 0 }" @click="auxiliaryChange(0)">
				<view class="label">普通用户</view>
				<view class="value">{{ z_num_1 }}</view>
			</view>
			<view class="item" :class="{ 'active': auxiliaryCurrent == 1 }" @click="auxiliaryChange(1)">
				<view class="label">VIP</view>
				<view class="value">{{ z_num_2 }}</view>
			</view>
			<view class="item" :class="{ 'active': auxiliaryCurrent == 2 }" @click="auxiliaryChange(2)">
				<view class="label">SVIP</view>
				<view class="value">{{ z_num_3 }}</view>
			</view>
			<view class="item" :class="{ 'active': auxiliaryCurrent == 3 }" @click="auxiliaryChange(3)">
				<view class="label">PLUS</view>
				<view class="value">{{ z_num_4 }}</view>
			</view>
		</view>
		<view class="auxiliary-box" v-else>
			<view class="item w-100 active">
				<view class="label">人数</view>
				<view class="value">{{ sanxia_count }}</view>
			</view>
		</view>
		<view class="list-box" v-if="tabCurrent != 'sanxia'">
			<view class="item" v-for="(item, index) in list" :key="index">
				<image class="avatar" :src="item.avatar" mode="aspectFill"></image>
				<view class="content">
					<view class="name u-line-1">{{ item.name }}</view>
					<view class="bottom">
						<text>ID：{{ item.id }}</text>
						<text class="ml-8">{{ item.create_time }}</text>
					</view>
				</view>
				<view class="tool-box" v-if="item.mobile">
					<view class="tool-item" @click="copyHandle(item.mobile)">
						<text class="iconfont icon-fuzhi"></text>
					</view>
					<view class="tool-item" @click="makePhoneCall(item.mobile)">
						<text class="iconfont icon-dianhua"></text>
					</view>
				</view>
			</view>
			
			<u-loadmore :status="status" />
		</view>
	</view>
</template>

<script>
	import { getMaterial, getTeamList } from '@/api/api'

	export default {
		data () {
			return {
				userInfo: {},
				
				tabCurrent: 'zhi',
				auxiliaryCurrent: 0,
				
				list: [],
				z_num_1: 0,
				z_num_2: 0,
				z_num_3: 0,
				z_num_4: 0,
				sanxia_count: 0,
				/* 分页 */
				status: 'loadmore',
				page: 0,
				last_page: 1,
			}
		},
		onLoad () {
			getMaterial().then(res => {
				if (res) {
					this.userInfo = res.data.data
				}
			})
			
			this.loadData()
		},
		onReachBottom () {
			this.getListData()
		},
		methods: {
			loadData () {
				this.page = 0
				this.last_page = 1
				this.list = []
				this.getListData()
			},
			async getListData () {
				if(this.page >= this.last_page) return
			
				this.status = 'loading'
				this.page = ++this.page
			
				const req = await getTeamList({
					type: this.tabCurrent,
					level: this.auxiliaryCurrent,
					page: this.page,
					page_size: 10,
				})
				
				if (req) {
					this.list = this.list.concat(req.data.data.data)
					this.last_page = req.data.data.last_page
					
					this.z_num_1 = req.data.z_num_1
					this.z_num_2 = req.data.z_num_2
					this.z_num_3 = req.data.z_num_3
					this.z_num_4 = req.data.z_num_4
					this.sanxia_count = req.data.sanxia_count;
					if (this.page >= this.last_page) {
						this.status = 'nomore'
					} else {
						this.status = 'loading'
					}
					
					return ''
				}
			},
			
			tabChange (index) {
				this.tabCurrent = index
				this.auxiliaryCurrent = 0
				
				this.loadData()
			},
			auxiliaryChange (index) {
				this.auxiliaryCurrent = index
				this.loadData()
			},
			copyHandle (data) {
				uni.setClipboardData({
					data,
					success: () => {
						
					}
				})
			},
			makePhoneCall (phoneNumber) {
				uni.makePhoneCall({
					phoneNumber
				})
			}
		}
	}
</script>

<style lang="scss">
.team-page {
	.top-box {
		padding: 72rpx 50rpx 0;
		background-color: #2d2c3f;
		.box {
			position: relative;
			.bg {
				width: 100%;
			}
			.content {
				position: absolute;
				top: 0;
				left: 0;
				right: 0;
				bottom: 0;
				z-index: 10;
				padding: 54rpx 36rpx 32rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				.user-info {
					display: flex;
					align-items: center;
					.avatar {
						box-sizing: content-box;
						width: 100rpx;
						height: 100rpx;
						border-radius: 50%;
						border: 2rpx solid #fff;
					}
					.info {
						margin-left: 22rpx;
						.name {
							color: #fff;
							font-size: 32rpx;
							font-weight: bold;
						}
						.grade {
							margin-top: 8rpx;
							border-radius: 30rpx;
							width: 100rpx;
							height: 30rpx;
							color: #fff;
							font-size: 22rpx;
							text-align: center;
							line-height: 30rpx;
							background-color: rgba(0, 0, 0, 0.3);
						}
					}
				}
				.id {
					color: #fff;
					font-size: 24rpx;
				}
			}
		}
	}
	.tabs-box {
		background-color: #fff;
		border-bottom: 1rpx solid #eee;
		display: flex;
		justify-content: center;
		.item {
			width: 200rpx;
			height: 80rpx;
			color: #000;
			font-size: 30rpx;
			text-align: center;
			line-height: 80rpx;
			position: relative;
			&.active {
				font-weight: bold;
				&::before {
					content: '';
					display: block;
					width: 62rpx;
					height: 6rpx;
					border-radius: 6rpx;
					background-color: #333;
					position: absolute;
					bottom: 0;
					left: 50%;
					transform: translateX(-50%);
				}
			}
		}
	}
	.auxiliary-box {
		display: flex;
		background-color: #f8f8f8;
		.item {
			width: 192rpx;
			padding: 26rpx 0;
			text-align: center;
			&.w-100 {
				width: 100%;
			}
			.label {
				color: #000;
				font-size: 26rpx;
			}
			.value {
				color: #666;
				font-size: 26rpx;
			}
			&.active {
				font-weight: bold;
				.label {
					color: #5749f7;
				}
				.value {
					color: #5749f7;
				}
			}
		}
	}
	.list-box {
		padding: 0 30rpx;
		.item {
			height: 138rpx;
			display: flex;
			align-items: center;
			.avatar {
				width: 84rpx;
				height: 84rpx;
				border-radius: 50%;
			}
			.content {
				flex: 1;
				margin-left: 26rpx;
				.name {
					color: #000;
					font-size: 30rpx;
				}
				.bottom {
					margin-top: 6rpx;
					color: #666;
					font-size: 22rpx;
				}
			}
			.tool-box {
				margin-left: 20rpx;
				display: flex;
				gap: 14rpx;
				.tool-item {
					width: 68rpx;
					height: 68rpx;
					border-radius: 50%;
					text-align: center;
					line-height: 68rpx;
					background-color: #efeefa;
					.iconfont {
						color: #5749f7;
						font-size: 36rpx;
					}
				}
			}
		}
	}
	.empty-box {
		margin-top: 160rpx;
	}
}
</style>
